<?php

namespace App\Models\Elasticsearch;

abstract class Model
{

    /**
     * 排序【升序】
     */
    const ORDER_ASC = 'asc';

    /**
     * 排序【降序】
     */
    const ORDER_DESC = 'desc';

    /**
     * 获取客户端
     *
     * @return \Elasticsearch\Client
     */
    protected function client()
    {
        return resolve(Client::class)->client();
    }

    /**
     * 查询数据
     *
     * @param array $params
     * @return array|callable
     */
    protected function search(array $params = [])
    {
        if (!isset($params['index'])) {
            $params['index'] = static::INDEX_NAME;
        }
        return $this->client()->search($params);
    }

    /**
     * Handle dynamic static method calls into the service.
     *
     * @param string $method
     * @param array $parameters
     * @return mixed
     */
    public static function __callStatic($method, $parameters)
    {
        return (new static)->$method(...$parameters);
    }

    /**
     * Handle dynamic method calls into the service.
     *
     * @param string $method
     * @param array $parameters
     * @return mixed
     */
    public function __call($method, $parameters)
    {
        return $this->$method(...$parameters);
    }
}
